﻿Public Class StructActiv
    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Public databaseName As String
    Private Sub StructActiv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""
        Me.ComboBoxCodeTyp.Visible = False
        Me.ComboBoxidCatg.Visible = False
        Me.TextBoxIdPop.Visible = False
        DataGridViewSuivPop.MultiSelect = False
        DataGridViewSuivPop.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewSuivPop.ReadOnly = True
        majCatg()
        miseajour()
    End Sub

    Private Sub majCatg()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[ClassifAge]"
            db.Connecter(databaseName)
            Me.ComboBoxIdCatg.Items.Clear()
            Me.ComboBoxNomCatg.Items.Clear()
            Me.ComboBoxIdCatg.Items.Add("")
            Me.ComboBoxNomCatg.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxIdCatg.Items.Add(db.rdr(0))
                Me.ComboBoxNomCatg.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxIdCatg.SelectedIndex = 0
            Me.ComboBoxNomCatg.SelectedIndex = 0
            db.Disconnecter()

            majTyp()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomCatg_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.TextChanged

        If Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomCatg.Text.Length
            Me.ComboBoxNomCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
            Me.ComboBoxNomCatg.SelectionStart = Pos
            Me.ComboBoxNomCatg.SelectionLength = Me.ComboBoxNomCatg.Text.Length - Pos
            Me.ComboBoxidCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
        Else
            Me.ComboBoxidCatg.Text = ""
        End If
    End Sub

    Private Sub majTyp()
        Try
            Entrer = ""
            If Not Me.ComboBoxNomCatg.Text = "Tout" Then
                db.myReq = "Select * FROM  [dbo].[CLASAGETYP] Where [Id_ClassifAge]='" & Me.ComboBoxidCatg.Text & "'"
            Else
                db.myReq = "Select * FROM  [dbo].[CLASAGETYP]"
            End If
            db.Connecter(databaseName)
            Me.ComboBoxCodeTyp.Items.Clear()
            Me.ComboBoxNomTyp.Items.Clear()
            Me.ComboBoxCodeTyp.Items.Add("")
            Me.ComboBoxNomTyp.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxCodeTyp.Items.Add(db.rdr(0))
                Me.ComboBoxNomTyp.Items.Add(db.rdr(3))
            End While
            Me.ComboBoxCodeTyp.SelectedIndex = 0
            Me.ComboBoxNomTyp.SelectedIndex = 0
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub ComboBoxNomTyp_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomTyp.TextChanged

        If Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomTyp.Text.Length
            Me.ComboBoxNomTyp.SelectedIndex = Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text)
            Me.ComboBoxNomTyp.SelectionStart = Pos
            Me.ComboBoxNomTyp.SelectionLength = Me.ComboBoxNomTyp.Text.Length - Pos
            Me.ComboBoxCodeTyp.SelectedIndex = Me.ComboBoxNomTyp.FindString(Me.ComboBoxNomTyp.Text)
        Else
            Me.ComboBoxCodeTyp.Text = ""
        End If
    End Sub

    Private Sub miseajour()
        Try
            db.myReq = "SELECT * FROM  [dbo].[StructActiv]"
            db.Connecter(databaseName)
            Me.DataGridViewSuivPop.Rows.Clear()
            While db.rdr.Read()
                DataGridViewSuivPop.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), "", Me.ComboBoxNomTyp.Items.Item(Me.ComboBoxCodeTyp.Items.IndexOf(db.rdr(2))), db.rdr(4), db.rdr(5))
            End While
            db.Disconnecter()
            If DataGridViewSuivPop.RowCount - 1 = 0 Then
                Me.TextBoxEff.Text = ""
                Me.TextBoxMasc.Text = ""
            Else
                DataGridViewSuivPop.Rows(0).Selected = True
                Me.TextBoxEff.Text = DataGridViewSuivPop.SelectedCells(6).Value
                Me.TextBoxMasc.Text = DataGridViewSuivPop.SelectedCells(7).Value
                Me.DateTimePickerDate.Text = DataGridViewSuivPop.SelectedCells(3).Value
                Me.ComboBoxNomTyp.SelectedItem = Me.ComboBoxCodeTyp.Items.IndexOf(DataGridViewSuivPop.SelectedCells(2).Value)

                db.myReq = "Select [Id_ClassifAge] FROM  [dbo].[CLASAGETYP] Where [Id_ClAge]='" & DataGridViewSuivPop.SelectedCells(2).Value & "'"
                db.Connecter(databaseName)
                While db.rdr.Read
                    Me.ComboBoxNomCatg.SelectedItem = Me.ComboBoxidCatg.Items.IndexOf(db.rdr(0))
                End While
                db.Disconnecter()
            End If

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub DataGridViewSuivPop_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewSuivPop.CurrentCellChanged
        If Entrer = "debut" Then
            selection()
        End If
    End Sub
    Private Sub DataGridViewSuivPop_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewSuivPop.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            selection()
        End If
    End Sub

    Private Sub selection()
        Try
            If Not DataGridViewSuivPop.SelectedCells(0).Value.ToString = "" Then

                If Not DataGridViewSuivPop.SelectedCells(3).Value.ToString = "" Then
                    Me.DateTimePickerDate.Text = DataGridViewSuivPop.SelectedCells(3).Value
                Else
                    Me.DateTimePickerDate.Text = ""
                End If
                If Not DataGridViewSuivPop.SelectedCells(6).Value.ToString = "" Then
                    Me.TextBoxEff.Text = DataGridViewSuivPop.SelectedCells(6).Value
                Else
                    Me.TextBoxEff.Text = ""
                End If
                If Not DataGridViewSuivPop.SelectedCells(7).Value.ToString = "" Then
                    Me.TextBoxMasc.Text = DataGridViewSuivPop.SelectedCells(7).Value
                Else
                    Me.TextBoxMasc.Text = ""
                End If

                If Not DataGridViewSuivPop.SelectedCells(2).Value.ToString = "" Then
                    Me.ComboBoxNomTyp.SelectedIndex = Me.ComboBoxCodeTyp.Items.IndexOf(DataGridViewSuivPop.SelectedCells(2).Value)
                    db.myReq = "Select [Id_ClassifAge] FROM  [dbo].[CLASAGETYP] Where [Id_ClAge]='" & DataGridViewSuivPop.SelectedCells(2).Value & "'"
                    db.Connecter(databaseName)
                    While db.rdr.Read
                        Me.ComboBoxNomCatg.SelectedIndex = Me.ComboBoxidCatg.Items.IndexOf(db.rdr(0))
                    End While
                    db.Disconnecter()
                Else
                    Me.ComboBoxNomCatg.SelectedItem = ""
                End If
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub


    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxEff.Text = ""
        Me.TextBoxMasc.Text = ""
    End Sub
    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Entrer = ""
            If action = "ajouter" Then
                db.myReq = "INSERT INTO  [dbo].[StructActiv]([Id_Pop],[Id_ClAge],[DATREC_SACT],[ACTIFSNB_SACT],[Male_SAct]) VALUES('" & Me.TextBoxIdPop.Text & "','" & Me.ComboBoxCodeTyp.Text & "','" & Me.DateTimePickerDate.Text & "','" & Me.TextBoxEff.Text & "','" & Me.TextBoxMasc.Text & "')"
                MsgBox(db.myReq)
            Else
                db.myReq = "UPDATE  [dbo].[StructActiv] SET [Id_Pop] = '" & Me.TextBoxIdPop.Text & "',[Id_ClAge] = '" & Me.ComboBoxCodeTyp.Text & "',[DATREC_SACT] = '" & Me.DateTimePickerDate.Text & "',[ACTIFSNB_SACT] = '" & Me.TextBoxEff.Text & "',[Male_SAct]= '" & Me.TextBoxMasc.Text & "' WHERE [Id_SAct]= '" & Me.DataGridViewSuivPop.SelectedCells(0).Value & "'"

            End If
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        miseajour()
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[StructActiv]  WHERE [Id_SAct]= '" & Me.DataGridViewSuivPop.SelectedCells(0).Value & "'"
            db.Connecter(databaseName)
            db.Disconnecter()
            miseajour()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub ButtonMod_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonMod.Click
        action = "update"
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim typ1 As New TypologieNiv1Form(databaseName)
        typ1.Text = "Catégorie"
        typ1.Table = "ClassifAge"
        typ1.Visible = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim typCat1 As New TypologieNiv1CategForm(databaseName)
        typCat1.Text = "typ cat 11"
        typCat1.table1 = "ClassifAge"
        typCat1.table2 = "ClasAgeTyp"
        typCat1.Visible = True
    End Sub

  

    Private Sub ComboBoxNomCatg_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.MouseDoubleClick
        majCatg()
    End Sub

    Private Sub ComboBoxNomTyp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxNomTyp.MouseDoubleClick
        majTyp()
    End Sub

End Class